//
//  MenuViewController.swift
//  AntProj
//
//  Created by 陈宇 on 2017/5/2.
//  Copyright © 2017年 🐜. All rights reserved.
//

import UIKit
import RxSwift

class MenuViewController: BaseViewController {
    
    @IBOutlet weak var heigConstraint: NSLayoutConstraint!
    @IBOutlet weak var tableView: UITableView!
    
    let menuItems = [("Map", BaseNavigationController(rootViewController: MapViewController())),
                     ("Me", BaseNavigationController(rootViewController: UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MeViewController"))),
                     ("Featured Listings", BaseNavigationController(rootViewController: FeaturedViewController())),
                     ("Listings", BaseNavigationController(rootViewController: ListingsViewController())),
                     ("Bidding", BaseNavigationController(rootViewController: BiddingsController())),
                     ("Feedback/Complaint", BaseNavigationController(rootViewController: FeedbackViewController())),
                     ("Need Advice?", BaseNavigationController(rootViewController: UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NeedAdviceViewController"))),
                     ("About", BaseNavigationController(rootViewController: UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AboutViewController")))]
    
    override func configureUI() {
        super.configureUI()
        
        tableView.layoutSubviews()
        heigConstraint.constant = tableView.contentSize.height
    }
    
}

extension MenuViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return menuItems.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
        cell.textLabel?.text = menuItems[indexPath.row].0
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        if indexPath.row != 0 && !UIApplication.isLogin {
            let vc = BaseNavigationController(rootViewController: LoginViewController {
                self.slideMenuController()?.changeMainViewController(self.menuItems[indexPath.row].1, close: true)
            })
            present(vc, animated: true, completion: nil)
            return
        }
        
        self.slideMenuController()?.changeMainViewController(menuItems[indexPath.row].1, close: true)
    }
}
